﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Database;
using System.Globalization;

namespace DT5_Anh09110006_Hung09110044_Vu09110157.QuanLy
{
    public partial class SuaLopHoc : System.Web.UI.Page
    {
        Business.QuanLyLopHoc qllophoc = new Business.QuanLyLopHoc();
        Business.QuanLyNhanVien qlnhanvien = new Business.QuanLyNhanVien();
        Business.QuanLyPhongHoc qlphonghoc = new Business.QuanLyPhongHoc();
        protected string ThoiGian = System.DateTime.Now.ToString("MM/dd/yyyy");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["MaLopHoc"] != null)
                {
                    loaddulieu();
                }
                else
                {
                    Response.Redirect("QuanLyLopHoc.aspx");
                }
            }
        }
        private void loaddulieu()
        {
            ddlGiaoVien.DataSource = qlnhanvien.LayDanhSachGiangVien();
            ddlGiaoVien.DataTextField = "HoTenNV";
            ddlGiaoVien.DataValueField = "MaNV";
            ddlGiaoVien.DataBind();
            ddlPhongHoc.DataSource = qlphonghoc.LayDanhSachPhong();
            ddlPhongHoc.DataTextField = "TenPhong";
            ddlPhongHoc.DataValueField = "MaPhong";
            ddlPhongHoc.DataBind();
            //Load du lieu
            String malophoc = Session["MaLopHoc"].ToString();
            LopHoc lop = qllophoc.LayLopHocTheoMa(malophoc);
             txtMaLop.Text=lop.MaLop;
             ddlGiaoVien.SelectedValue=lop.GiaoVienPT;
             ddlPhongHoc.SelectedValue=lop.PhongHoc;
             txtHocPhi.Text=lop.HocPhi.ToString();
             txtSiSo.Text=lop.SiSo.ToString();
             ThoiGian = lop.NgayKhaiGiang.Value.ToString("MM/dd/yyyy");
              txtLichHoc.Text=lop.LichHoc;
             txtMota.Text=lop.MoTa;
        }

        protected void btnCapNhatLopHoc_Click(object sender, EventArgs e)
        {
            Session["MaLopHoc"] = null;
            LopHoc lop = new LopHoc
            {
                MaLop = txtMaLop.Text,
                GiaoVienPT = ddlGiaoVien.SelectedValue,
                PhongHoc = ddlPhongHoc.SelectedValue,
                HocPhi = int.Parse(txtHocPhi.Text),
                SiSo = int.Parse(txtSiSo.Text),
                NgayKhaiGiang = System.DateTime.ParseExact(Request["NgayKhaiGiang"], "MM/dd/yyyy", CultureInfo.InvariantCulture),
                LichHoc = txtLichHoc.Text,
                MoTa = txtMota.Text
            };
            if (qllophoc.SuaLopHoc(lop))
            {

                Response.Write("<script language='javascript' type='text/javascript'>"
                                  + " alert('Chỉnh sửa thành công');"
                                  + "window.location='QuanLyLopHoc.aspx'"
                                  + "</script>");

            }
            else
            {
                Response.Write("<script language='javascript' type='text/javascript'>"
                                + "alert('Không thể chỉnh sửa lớp học này');"
                                + "window.location='QuanLyLopHoc.aspx'"
                                + "</script>");
            }
            
           

        }

        protected void btnQuayLai_Click(object sender, EventArgs e)
        {
            Session["MaLopHoc"] = null;
            Response.Redirect("QuanLyLopHoc.aspx");
        }
    }
}